package com.lwl.service.impl.scl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.dao.scl.TSclRecordDetailsMapper;
import com.lwl.dao.scl.TSclSubMapper;
import com.lwl.entity.MyPage;
import com.lwl.entity.scl.TSclRecordDetails;
import com.lwl.entity.scl.TSclSub;
import com.lwl.service.scl.TSclSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (TSclSub)表服务实现类
 *
 * @author makejava
 * @since 2022-10-28 18:09:14
 */
@Service
public class TSclSubServiceImpl implements TSclSubService {
   @Autowired
   private TSclSubMapper tSclSubMapper;
   @Autowired
   private TSclRecordDetailsMapper tSclRecordDetailsMapper;

   @Override
   public PageInfo queryAll(MyPage myPage, TSclSub tSclSub) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TSclSub> tSclSubs = tSclSubMapper.queryAll(tSclSub);
      return new PageInfo(tSclSubs);
   }

   @Override
   public Boolean importExcel(MultipartFile file) {
      int i = tSclSubMapper.selectCount(null);
      if (i>0){
         return false;
      }else {
         InputStream is = null;
         try {
            is = file.getInputStream();
         } catch (IOException e) {
            e.printStackTrace();
         }
         //1.进行读取数据，TSclSub是我的pojo类，
         //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
         //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
         //EasyExcel.read(is, TSclSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
         EasyExcel.read(is, TSclSub.class, new AnalysisEventListener<TSclSub>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TSclSub> cachedDataList = new ArrayList<>();

            @Override
            public void invoke(TSclSub data, AnalysisContext context) {
               cachedDataList.add(data);
               if (cachedDataList.size() >= BATCH_COUNT) {
                  saveData();
                  // 存储完成清理 list
                  cachedDataList = new ArrayList<>();
               }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
               saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
            //log.info("{}条数据，开始存储数据库！", cachedDataList.size());
            //log.info("存储数据库成功！");
            save(cachedDataList);
            }
         }).sheet("SCL量表").doRead();
         return true;
      }
   }


   @Override
   public void save(List<TSclSub> list) {
      tSclSubMapper.insertSCLTitle(list);
   }

   @Override
   public Boolean addOrUpdataSCLTitle(TSclSub tSclSub) {
      int i = -1;
      if (tSclSub.getId()==null) {
         i = tSclSubMapper.insert(tSclSub);
      }else {
         i = tSclSubMapper.updateByPrimaryKey(tSclSub);
      }
      return i>=0?true:false;
   }

   @Override
   public Boolean delSclTitle(Integer sclnumber) {
      int i=-1;
      TSclRecordDetails tSclRecordDetails = new TSclRecordDetails();
      tSclRecordDetails.setSclnumber(sclnumber);
      int i1 = tSclRecordDetailsMapper.selectCount(tSclRecordDetails);
      System.out.println("该题目已经被多少人做 = " + i1);
      if (i1 == 0){
         TSclSub tSclSub = new TSclSub();
         tSclSub.setSclnumber(sclnumber);
         i = tSclSubMapper.delete(tSclSub);
      }
      return i>=0?true:false;
   }

   @Override
   public List<Integer> delSclnumbers(Integer[] sclnumbers) {
      List<Integer> dellist = new ArrayList();
      List<Integer> nodellist = new ArrayList();
      for (Integer sclnumber : sclnumbers) {
         TSclRecordDetails tSclRecordDetails = new TSclRecordDetails();
         tSclRecordDetails.setSclnumber(sclnumber);
         int i = tSclRecordDetailsMapper.selectCount(tSclRecordDetails);
         System.out.println(sclnumber+"题已经被"+i+"人做");
         if (i == 0){
            dellist.add(sclnumber);
         }else {
            nodellist.add(sclnumber);
         }
      }
      if (dellist.size()>0){
         int i = tSclSubMapper.delSclnumbers(dellist);
         System.out.println("删除行数为i ================== " + i);
      }else {
         System.out.println("删除行数为i ================== 0");
      }
      return nodellist;
   }

   @Override
   public Boolean checkSclNumber(Integer sclnumber) {
      TSclSub tSclSub = new TSclSub();
      tSclSub.setSclnumber(sclnumber);
      TSclSub tSclSub1 = tSclSubMapper.selectOne(tSclSub);
      return tSclSub1==null;
   }

   @Override
   public PageInfo getIndexAllSub(MyPage myPage) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TSclSub> tSclSubs = tSclSubMapper.selectAll();
      System.out.println("tSclSubs = " + tSclSubs);
      return new PageInfo(tSclSubs);
   }
}
